package top.weiliang.springboot3.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 控制器日志切面
 *
 * @author weiliang
 */

@Aspect
@Component
@Slf4j
@Order(10)
public class ControllerLogger implements ControllerPointCut{

    @Before("pointcut()")
    public void before(JoinPoint jp){
        //拦截的方法信息
        log.debug("控制器切面====>{}", jp.getSignature());
        Object[] args = Optional.ofNullable(jp.getArgs()).orElse(new Object[]{});
        StringBuilder sb = new StringBuilder();
        for (Object arg : args) {
            sb.append(arg.getClass().getName()).append(" ").append(arg).append(",");
        }
        if (!sb.isEmpty()) {
            sb.deleteCharAt(sb.length() - 1);
            log.debug("参数信息: {}", sb);
        }
    }
}
